package com.sport.controller;

import com.google.gson.Gson;
import com.sport.context.BaseContext;
import com.sport.pojo.dto.UpdateUserDto;
import com.sport.pojo.dto.UserShow;
import com.sport.pojo.entity.DoubleToken;
import com.sport.pojo.entity.User;
import com.sport.pojo.vo.HistoryVo;
import com.sport.pojo.vo.UserAllInfoVo;
import com.sport.pojo.vo.VerifyVo;
import com.sport.result.Result;
import com.sport.service.UserService;
import com.sport.utils.JwtUtil;
import com.sport.properties.JwtProperties;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.sport.constant.MessageConstant.*;

@RestController
@RequestMapping("/user")
@Api(tags = "账号相关接口")
public class UserController {
    @Autowired
    private JwtProperties jwtProperties;

    @Autowired
    private UserService userService;

    @ApiOperation("登录")
    @GetMapping("/login")
    public Result login(String userAccount, String userPassword){
        UserAllInfoVo user = userService.login(userAccount, userPassword);
        Map<String,Object> claims = new HashMap<>();
        claims.put("userId",user.getUserId());
        String shortJwt = JwtUtil.createJWT(jwtProperties.getSecretKey(),
                jwtProperties.getShortTK(),
                claims);
        String LongJwt = JwtUtil.createJWT(jwtProperties.getSecretKey(),
                jwtProperties.getLongTK(),
                claims);
        return Result.success(new Gson().toJson(new DoubleToken(shortJwt,LongJwt,user)));
    }
    @ApiOperation("注册")
    @PostMapping ("/register")
    public Result register(@RequestBody User user){
        //TODO 注册后生成默认用户名和头像背景slogan 等信息
        int info = userService.register(user);
        if (info==0){
            return Result.error(FAILS_REGISTER);
        }else {
            System.out.println(info);
            Gson gson = new Gson();
            String s = gson.toJson(user);
            return Result.success(s);
            }
    }
    @ApiOperation("校验jwt")
    @GetMapping("/verify")
    public Result verify(){
        return Result.success();
    }

    @ApiOperation("查询用户信息")
    @PostMapping ("/findUserMessage")
    public Result findUserMessage(String userId){
        User user = userService.findUserMessage(userId);
        Gson gson = new Gson();
        String s = gson.toJson(user);
        if (user!=null){
            return Result.success(s);
        }else {
            return Result.error(FIND_USERMESSAGE_ERROR);
        }
    }

    @ApiOperation("用户修改密码")
    @GetMapping("/updatePassword")
    public Result updatePassword(String password){
        if(userService.updatePassword(password)!=0){
            return Result.success("修改成功");
        }
       return Result.error("修改失败");
    }
    @ApiOperation("用户修改信息")
    @PutMapping("/updateUserMessage")
    public Result userUpdateMessage(@RequestBody  UpdateUserDto updateUserDto){
        int info = userService.userUpdateMessage(updateUserDto);
        if (info!=0){
            return Result.success("修改成功");
        }else {
            return Result.error(UPDATE_USERMESSAGE_ERROR);
        }
    }
    @ApiOperation("展示历史战绩")
    @GetMapping("/showHistory")
    public Result showHistory(){
        List<HistoryVo> historyVos = userService.showHistory(BaseContext.getCurrentId());
        Gson gson = new Gson();
        String historyVosJson = gson.toJson(historyVos);
        return Result.success(historyVosJson);
        }
    @ApiOperation("查询足球段位排行榜")
    @GetMapping ("/showFootballDanRank")
    public Result showUserFootballDanRank(){
        List<UserShow> list = userService.showUserFootballDanRank();
        Gson gson = new Gson();
        String s = gson.toJson(list);
        if (list!=null){
            return Result.success(s);
        }else {
            return Result.error("查询失败");
        }
    }
    @ApiOperation("查询认证")
    @GetMapping("/findVerify")
    public Result findVerify(){
        VerifyVo verifyVo =userService.findVerify();
        return Result.success(new Gson().toJson(verifyVo));
    }

    @ApiOperation("查询段位")
    @GetMapping("/getDao")
    public Result getDao(){
        return Result.success(String.valueOf(userService.getDao()));
    }


}

